package rpg;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.TiledLayer;
import java.io.*;

public class Map extends LayerManager
{
	public Image im;
	public byte[] GroundLayer_cells;
	public byte[] TreeLayer_cells;
	public int[] x;
	public int[] y;
	public Image imgMark;
	public Image imgNPC;
	public Image imgNPC1;
	public Image imgNPC2;
	public Image imgNPC3;
	public Sprite []NPC;
	byte row;
	byte col;
	public void readmap(byte n){
		
			  try{
				  im=null;
				  im.createImage("/map"+n+".png");
				  InputStream is=this.getClass().getResourceAsStream("/map"+n+".dat");
				  DataInputStream dis=new DataInputStream(is);
				  row=dis.readByte();
				  col=dis.readByte();				  
				  GroundLayer_cells=new byte[row*col];
				  TreeLayer_cells=new byte[row*col];				  
				  for(int i=0;i<row*col;++i)
				  {
					   GroundLayer_cells[i]=dis.readByte();
				  }
				  for(int i=0;i<row*col;++i)
				  {
					   TreeLayer_cells[i]=dis.readByte();
				  }
				  dis.close();
				  is.close();
				  }catch(Exception e){System.out.println("error");}
	}
     Image TreeLayer_tiles;
     TiledLayer TreeLayer;
     Image GroundLayer_tiles;
     TiledLayer GroundLayer;    
     public Map(byte n)
     {
    	 NPC=null;
    	 imgMark=null;
    	 imgNPC1=null;
    	 imgNPC2=null;
    	 imgNPC3=null;
    	 readmap(n);
          try
          {
        	  if(Mapcanvas.curScreening<30)
        		  imgMark=Image.createImage("/imgMark.png");
        	  else
        		  imgMark=Image.createImage("/imgMark1.png");
        	  if(imgNPC==null)
        		  imgNPC=Image.createImage("/npc.png");
              Init(n);
               
          }
          catch (Exception ex)
          {
          	System.out.println("error1");
          }
     }
     void Init(byte n) throws Exception
     {
          TreeLayer_tiles = Image.createImage("/map"+n+".png");
          TreeLayer = new TiledLayer(col, row, TreeLayer_tiles, 24, 32);
          GroundLayer_tiles = Image.createImage("/map"+n+".png");
          GroundLayer = new TiledLayer(col, row, GroundLayer_tiles, 24, 32);
          fillLayer(TreeLayer, TreeLayer_cells);
          fillLayer(GroundLayer, GroundLayer_cells);
          if(n==32)
        	  addNPC(n);
          else if(n==2){
        	  imgNPC1=Image.createImage(imgNPC,96,0,24,32,0);
    		  imgNPC2=Image.createImage(imgNPC,120,0,24,32,0);
    		  imgNPC3=Image.createImage(imgNPC,144,0,24,32,0);
    		  NPC=new Sprite[9];
    		  NPC[0]=new Sprite(imgMark);
    		  NPC[1]=new Sprite(imgMark);
    		  NPC[2]=new Sprite(imgMark);
    		  NPC[3]=new Sprite(imgMark);
    		  NPC[4]=new Sprite(imgMark);
    		  NPC[5]=new Sprite(imgMark);
    		  NPC[6]=new Sprite(imgNPC1);
    		  NPC[7]=new Sprite(imgNPC2);
    		  NPC[8]=new Sprite(imgNPC3);
        	  append(NPC[6]);
        	  NPC[6].setPosition(240,32);
        	  append(NPC[7]);
        	  NPC[7].setPosition(148, 480);
        	  append(NPC[8]);
        	  NPC[8].setPosition(148, 544);
          }
          append(TreeLayer);
          append(GroundLayer);
          if(n!=32)
        	  addNPC(n);  
          if(n==32){
        	  append(NPC[3]);
    		  NPC[3].setPosition(192,32);
          }
          
     }
  void fillLayer(TiledLayer layer, byte[] cells) 
  {
        for(int row=0; row < layer.getRows(); ++row)
        {
            for(int col=0; col < layer.getColumns(); ++col)
            {
                layer.setCell(col, row, cells[row * layer.getColumns() + col]);
            }
        }
        
 }
  
  void addNPC(byte n)
  {
	  if(n==1)
	  {
		  NPC=new Sprite[9];
		  NPC[0]=new Sprite(imgMark);
		  NPC[1]=new Sprite(imgMark);
		  NPC[2]=new Sprite(imgMark);
		  NPC[3]=new Sprite(imgMark);
		  NPC[4]=new Sprite(imgMark);
		  NPC[5]=new Sprite(imgMark);
		  append(NPC[0]);
		  NPC[0].setPosition(28, 48);
		  append(NPC[1]);
		  NPC[1].setPosition(28, 208);
		  append(NPC[2]);
		  NPC[2].setPosition(192, 208);
		  append(NPC[3]);
		  NPC[3].setPosition(340, 208);
		  append(NPC[4]);
		  NPC[4].setPosition(438, 48);
		  append(NPC[5]);
		  NPC[5].setPosition(560, 208);
	  }
	  else if(n==2){
		  append(NPC[0]);
		  NPC[0].setPosition(360,352);
		  append(NPC[1]);
		  NPC[1].setPosition(246, 480);
		  append(NPC[2]);
		  NPC[2].setPosition(336,160);
		  append(NPC[3]);
		  NPC[3].setPosition(72,64);
		  append(NPC[4]);
		  NPC[4].setPosition(48,416);
		  append(NPC[5]);
		  NPC[5].setPosition(168, 416);
	  }
	  else if(n==32){
		  imgNPC1=Image.createImage(imgNPC,0,0,48,32,0);
		  imgNPC2=Image.createImage(imgNPC,48,0,24,32,0);
		  imgNPC3=Image.createImage(imgNPC,72,0,24,32,0);
		  NPC=new Sprite[9];
		  NPC[0]=new Sprite(imgNPC1);
		  NPC[1]=new Sprite(imgNPC2);
		  NPC[2]=new Sprite(imgNPC3);
		  NPC[3]=new Sprite(imgMark);
		  NPC[4]=new Sprite(imgMark);
		  NPC[5]=new Sprite(imgMark);
		  append(NPC[0]);
		  NPC[0].setPosition(144, 64);
		  append(NPC[1]);
		  NPC[1].setPosition(192, 64);
		  append(NPC[2]);
		  NPC[2].setPosition(168, 96);
	  }
	  else if(n>32){
		  NPC=new Sprite[9];
		  NPC[0]=new Sprite(imgMark);
		  NPC[1]=new Sprite(imgMark);
		  NPC[2]=new Sprite(imgMark);
		  NPC[3]=new Sprite(imgMark);
		  NPC[4]=new Sprite(imgMark);
		  NPC[5]=new Sprite(imgMark);
		  append(NPC[0]);
		  NPC[0].setPosition(108,288);
		  if(n==36){
			  append(NPC[1]);
			  NPC[1].setPosition(48, 96);
		  }
		  else if(n==33||n==34){
			  append(NPC[1]);
			  NPC[1].setPosition(96, 96);
		  }
		  else if(n==35){
			  append(NPC[1]);
			  NPC[1].setPosition(168,128);
			  append(NPC[2]);
			  NPC[2].setPosition(48, 128);
			  append(NPC[3]);
			  NPC[3].setPosition(96,64);
			  append(NPC[4]);
			  NPC[4].setPosition(120,64);
		  }
	  }
  }
}
